import java.util.TreeSet;

/**
 * @program: LeetCode
 * @description: LeetCode : 剑指 Offer II 057. 值和下标之差都在给定的范围内
 * @author: WXY
 * @create: 2023-01-05 11:22
 * @Version 1.0
 **/
public class offer_nineteen2_containsNearbyAlmostDuplicate {
    public static boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
        TreeSet<Long> set = new TreeSet<>();
        int len = nums.length;
        for (int i = 0; i < len; i++) {
            Long ceiling = set.ceiling((long) nums[i] - (long) t);
            if ((ceiling != null) && ceiling <= (long) nums[i] + (long) t) {
                return true;
            }
            set.add((long) nums[i]);
            if (i >= k) {
                set.remove((long) nums[i - k]);
            }
        }
        return false;
    }
}
